﻿using Entity;
using GL_CommonPrint.DataModels;
using Newtonsoft.Json;
using NPOI.HSSF.UserModel;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Spire.Xls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Configuration;
using System.Windows.Media;
using static GL_CommonPrint.ExcelGenerator;
using static GL_CommonPrint.FilePath;
using Color = System.Drawing.Color;

namespace GL_CommonPrint
{
    public partial class FrontReader
    {


        ReturnResult rsl;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="file">上传的文件</param>
        /// <param name="jsonStr">上传的Json字符串</param>
        /// <returns></returns>
        public ReturnResult Upload_XLSX_File(HttpPostedFileBase file, string jsonStr = "")
        {
            // 保存Excel文件
            var fullPath = getFullPath(GL_FileType.TemplateExcel, file.FileName);
            file.SaveAs(fullPath);

            return Upload_Json_String(file.FileName, jsonStr);
        }
        public ReturnResult UploadModel_UsingTemplate(string fileName, GL_WorkBook workbook, string templateName)
        {
            var filePath = getFullPath(GL_FileType.TemplateExcel, fileName);
            var templatePath = getFullPath(GL_FileType.TemplateExcel, templateName);
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            File.Copy(templatePath, filePath);

            return UploadWorkbookDataModel(fileName, workbook);

        }
        public ReturnResult UploadWorkbookDataModel(string fileName, GL_WorkBook workbook)
        {
            return Upload_Json_String(fileName, JsonConvert.SerializeObject(workbook));
        }
        public ReturnResult Upload_Json_String(string fileName, string jsonStr = "")
        {
            rsl = new ReturnResult();
            try
            {
                var fullPath = getFullPath(GL_FileType.TemplateExcel, fileName);

                // Json蒙版修改原文件
                Save_Json_String(jsonStr, fullPath);

                // 转换为PDF文件并保存
                var pdfPath = getFullPath(GL_FileType.PDF, Path.GetFileNameWithoutExtension(fileName) + ".pdf");
                var IsConverted = new FileFormatConverter().ExcelToPDF(fullPath, pdfPath);

                rsl.Message = "上传成功";
                rsl.Data = fileName;
                rsl.Result = true;

                return rsl;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
                rsl.Data = ex.StackTrace;

                return rsl;
            }
        }

        public ReturnResult Save_Json_String(string jsonStr, string excelPath)
        {
            if (string.IsNullOrEmpty(jsonStr))
            {
                return new ReturnResult
                {

                };
            }

            return new ExcelGenerator().Create(jsonStr, excelPath);
        }

    }

    public partial class FrontReader
    {
        public MemoryStream GetPDF(string fileName)
        {
            fileName = Path.GetFileNameWithoutExtension(fileName) + ".pdf";

            var fullPath = getFullPath(GL_FileType.PDF, fileName);

            var file = File.ReadAllBytes(fullPath);

            var ms = new MemoryStream(file);

            return ms;
        }
    }
}
